﻿
USE [Sector7]

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON

/*
 * DROP CONSTRAINT
 */
	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Employee_RS_EmployeeGroup]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Employee]'))
	ALTER TABLE [dbo].[RS_Employee] DROP CONSTRAINT [FK_RS_Employee_RS_EmployeeGroup]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Employee_S7_Citizen]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Employee]'))
	ALTER TABLE [dbo].[RS_Employee] DROP CONSTRAINT [FK_RS_Employee_S7_Citizen]
	
	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Customer_RS_CustomerGroup]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Customer]'))
	ALTER TABLE [dbo].[RS_Customer] DROP CONSTRAINT [FK_RS_Customer_RS_CustomerGroup]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Customer_S7_Citizen]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Customer]'))
	ALTER TABLE [dbo].[RS_Customer] DROP CONSTRAINT [FK_RS_Customer_S7_Citizen]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Supplier_S7_Citizen]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Supplier]'))
	ALTER TABLE [dbo].[RS_Supplier] DROP CONSTRAINT [FK_RS_Supplier_S7_Citizen]
	
	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Table_RS_Room]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Table]'))
	ALTER TABLE [dbo].[RS_Table] DROP CONSTRAINT [FK_RS_Table_RS_Room]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Metadata_RS_MetadataType]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Metadata]'))
	ALTER TABLE [dbo].[RS_Metadata] DROP CONSTRAINT [FK_RS_Metadata_RS_MetadataType]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Stock_RS_Metadata]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Stock]'))
	ALTER TABLE [dbo].[RS_Stock] DROP CONSTRAINT [FK_RS_Stock_RS_Metadata]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Stock_RS_Unit]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Stock]'))
	ALTER TABLE [dbo].[RS_Stock] DROP CONSTRAINT [FK_RS_Stock_RS_Unit]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_Stock_RS_Unit1]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_Stock]'))
	ALTER TABLE [dbo].[RS_Stock] DROP CONSTRAINT [FK_RS_Stock_RS_Unit1]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_MenuItem_RS_Metadata]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_MenuItem]'))
	ALTER TABLE [dbo].[RS_MenuItem] DROP CONSTRAINT [FK_RS_MenuItem_RS_Metadata]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_MenuItem_RS_Unit]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_MenuItem]'))
	ALTER TABLE [dbo].[RS_MenuItem] DROP CONSTRAINT [FK_RS_MenuItem_RS_Unit]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_PurchaseBill_RS_Employee]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_PurchaseBill]'))
	ALTER TABLE [dbo].[RS_PurchaseBill] DROP CONSTRAINT [FK_RS_PurchaseBill_RS_Employee]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_PurchaseBill_RS_Employee1]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_PurchaseBill]'))
	ALTER TABLE [dbo].[RS_PurchaseBill] DROP CONSTRAINT [FK_RS_PurchaseBill_RS_Employee1]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_PurchaseBillItem_RS_Metadata]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_PurchaseBillItem]'))
	ALTER TABLE [dbo].[RS_PurchaseBillItem] DROP CONSTRAINT [FK_RS_PurchaseBillItem_RS_Metadata]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_PurchaseBillItem_RS_PurchaseBill]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_PurchaseBillItem]'))
	ALTER TABLE [dbo].[RS_PurchaseBillItem] DROP CONSTRAINT [FK_RS_PurchaseBillItem_RS_PurchaseBill]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_PurchaseBillItem_RS_Unit]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_PurchaseBillItem]'))
	ALTER TABLE [dbo].[RS_PurchaseBillItem] DROP CONSTRAINT [FK_RS_PurchaseBillItem_RS_Unit]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_SalesBill_RS_Employee]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_SalesBill]'))
	ALTER TABLE [dbo].[RS_SalesBill] DROP CONSTRAINT [FK_RS_SalesBill_RS_Employee]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_SalesBill_RS_Employee1]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_SalesBill]'))
	ALTER TABLE [dbo].[RS_SalesBill] DROP CONSTRAINT [FK_RS_SalesBill_RS_Employee1]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_SalesBill_RS_Table]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_SalesBill]'))
	ALTER TABLE [dbo].[RS_SalesBill] DROP CONSTRAINT [FK_RS_SalesBill_RS_Table]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_SalesBillItem_RS_MenuItem]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_SalesBillItem]'))
	ALTER TABLE [dbo].[RS_SalesBillItem] DROP CONSTRAINT [FK_RS_SalesBillItem_RS_MenuItem]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_SalesBillItem_RS_SalesBill]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_SalesBillItem]'))
	ALTER TABLE [dbo].[RS_SalesBillItem] DROP CONSTRAINT [FK_RS_SalesBillItem_RS_SalesBill]

	IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_RS_SalesBillItem_RS_Unit]') AND parent_object_id = OBJECT_ID(N'[dbo].[RS_SalesBillItem]'))
	ALTER TABLE [dbo].[RS_SalesBillItem] DROP CONSTRAINT [FK_RS_SalesBillItem_RS_Unit]
	
/*
 * [dbo].[S7_Company]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[S7_Company]') AND type in (N'U'))
	DROP TABLE [dbo].[S7_Company]

	CREATE TABLE [dbo].[S7_Company](
		[UniqueId] [nchar](36) NOT NULL,
		[Name] [nchar](128) NOT NULL,
		[Introduce] [nchar](1024) NOT NULL,
		[Address] [nchar](1024) NOT NULL,
		[Postcode] [nchar](10) NOT NULL,
		[Telephone] [nchar](32) NOT NULL,
		[Logo] [image] NULL,
		[Map] [nchar](1024) NULL,
	CONSTRAINT [PK_S7_Company] PRIMARY KEY CLUSTERED 
	(
		[UniqueId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

/*
 * [dbo].[S7_Citizen]
 */ 
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[S7_Citizen]') AND type in (N'U'))
	DROP TABLE [dbo].[S7_Citizen]

	CREATE TABLE [dbo].[S7_Citizen](
		[UniqueId] [bigint] IDENTITY(1,1) NOT NULL,
		[CardId] [nchar](20) NULL,
		[FirstName] [nchar](10) NOT NULL,
		[LastName] [nchar](10) NOT NULL,
		[Birthday] [date] NOT NULL,
		[UserName] [nchar](32) NULL,
		[Password] [nchar](32) NULL,
		[MobilePhone] [nchar](16) NOT NULL,
		[Telephone] [nchar](16) NULL,
		[PersonalId] [nchar](18) NULL,
		[Gender] [nchar](10) NULL,
		[Residence] [nchar](512) NULL,
		[Address] [nchar](512) NULL,
	 CONSTRAINT [PK_S7_Citizen] PRIMARY KEY CLUSTERED 
	(
		[UniqueId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]
	
/*
 * [dbo].[RS_Seed]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Seed]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Seed]

	CREATE TABLE [dbo].[RS_Seed](
		[TableName] [nchar](32) NOT NULL,
		[Seed] [int] NOT NULL
	) ON [PRIMARY]

/*
 * [dbo].[RS_EmployeeGroup]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_EmployeeGroup]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_EmployeeGroup]

	CREATE TABLE [dbo].[RS_EmployeeGroup](
		[GroupId] [int] IDENTITY(1,1) NOT NULL,
		[GroupName] [nchar](32) NOT NULL,
		[BonusRate] [decimal](4, 4) NOT NULL,
		[Permission] [bigint] NOT NULL,
	 CONSTRAINT [PK_RS_EmployeeGroup] PRIMARY KEY CLUSTERED 
	(
		[GroupId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

/*
 * [dbo].[RS_Employee]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Employee]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Employee]

	CREATE TABLE [dbo].[RS_Employee](
		[UniqueId] [bigint] NOT NULL,
		[GroupId] [int] NOT NULL,
		[IsValid] [bit] NOT NULL,
		[BasicSalary] [decimal](18, 2) NOT NULL,
	 CONSTRAINT [PK_RS_Employee] PRIMARY KEY CLUSTERED 
	(
		[UniqueId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_Employee]  WITH CHECK ADD  CONSTRAINT [FK_RS_Employee_RS_EmployeeGroup] FOREIGN KEY([GroupId])
	REFERENCES [dbo].[RS_EmployeeGroup] ([GroupId])
	ALTER TABLE [dbo].[RS_Employee] CHECK CONSTRAINT [FK_RS_Employee_RS_EmployeeGroup]

	ALTER TABLE [dbo].[RS_Employee]  WITH CHECK ADD  CONSTRAINT [FK_RS_Employee_S7_Citizen] FOREIGN KEY([UniqueId])
	REFERENCES [dbo].[S7_Citizen] ([UniqueId])
	ALTER TABLE [dbo].[RS_Employee] CHECK CONSTRAINT [FK_RS_Employee_S7_Citizen]

/*
 * [dbo].[RS_CustomerGroup]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_CustomerGroup]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_CustomerGroup]

	CREATE TABLE [dbo].[RS_CustomerGroup](
		[GroupId] [int] IDENTITY(1,1) NOT NULL,
		[GroupName] [nchar](32) NOT NULL,
		[Rebate] [decimal](4, 4) NOT NULL,
		[Permission] [bigint] NOT NULL,
	 CONSTRAINT [PK_RS_CustomerGroup] PRIMARY KEY CLUSTERED 
	(
		[GroupId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

/*
 * [dbo].[RS_Customer]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Customer]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Customer]

	CREATE TABLE [dbo].[RS_Customer](
		[UniqueId] [bigint] NOT NULL,
		[GroupId] [int] NOT NULL,
		[IsValid] [bit] NOT NULL,
	 CONSTRAINT [PK_RS_Customer] PRIMARY KEY CLUSTERED 
	(
		[UniqueId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_Customer]  WITH CHECK ADD  CONSTRAINT [FK_RS_Customer_RS_CustomerGroup] FOREIGN KEY([GroupId])
	REFERENCES [dbo].[RS_CustomerGroup] ([GroupId])
	ALTER TABLE [dbo].[RS_Customer] CHECK CONSTRAINT [FK_RS_Customer_RS_CustomerGroup]

	ALTER TABLE [dbo].[RS_Customer]  WITH CHECK ADD  CONSTRAINT [FK_RS_Customer_S7_Citizen] FOREIGN KEY([UniqueId])
	REFERENCES [dbo].[S7_Citizen] ([UniqueId])
	ALTER TABLE [dbo].[RS_Customer] CHECK CONSTRAINT [FK_RS_Customer_S7_Citizen]

/*
 * [dbo].[RS_Supplier]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Supplier]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Supplier]

	CREATE TABLE [dbo].[RS_Supplier](
		[SupplierId] [int] IDENTITY(1,1) NOT NULL,
		[SupplierName] [nchar](128) NOT NULL,
		[Telephone] [nchar](16) NOT NULL,
		[MobilePhone] [nchar](16) NOT NULL,
		[ProprietorId] [bigint] NULL,
		[Address] [nchar](512) NOT NULL,
	 CONSTRAINT [PK_RS_Supplier] PRIMARY KEY CLUSTERED 
	(
		[SupplierId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_Supplier]  WITH CHECK ADD  CONSTRAINT [FK_RS_Supplier_S7_Citizen] FOREIGN KEY([ProprietorId])
	REFERENCES [dbo].[S7_Citizen] ([UniqueId])
	ALTER TABLE [dbo].[RS_Supplier] CHECK CONSTRAINT [FK_RS_Supplier_S7_Citizen]

/*
 * [dbo].[RS_Unit]
 */
 	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Unit]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Unit]

	CREATE TABLE [dbo].[RS_Unit](
		[UnitId] [int] IDENTITY(1,1) NOT NULL,
		[UnitName] [nchar](32) NOT NULL,
	 CONSTRAINT [PK_RS_Unit] PRIMARY KEY CLUSTERED 
	(
		[UnitId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

/*
 * [dbo].[RS_Room]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Room]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Room]

	CREATE TABLE [dbo].[RS_Room](
		[RoomId] [int] IDENTITY(1,1) NOT NULL,
		[RoomName] [nchar](32) NOT NULL,
		[RoomType] [int] NOT NULL,
	 CONSTRAINT [PK_RS_Room] PRIMARY KEY CLUSTERED 
	(
		[RoomId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

/*
 * [dbo].[RS_Table]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Table]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Table]

	CREATE TABLE [dbo].[RS_Table](
		[TableId] [int] IDENTITY(1,1) NOT NULL,
		[RoomId] [int] NULL,
		[TableNo] [int] NOT NULL,
		[SeatCount] [int] NOT NULL,
		[Status] [int] NOT NULL,
	 CONSTRAINT [PK_RS_Table] PRIMARY KEY CLUSTERED 
	(
		[TableId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_Table]  WITH CHECK ADD  CONSTRAINT [FK_RS_Table_RS_Room] FOREIGN KEY([RoomId])
	REFERENCES [dbo].[RS_Room] ([RoomId])
	ALTER TABLE [dbo].[RS_Table] CHECK CONSTRAINT [FK_RS_Table_RS_Room]
	
/*
 * [dbo].[RS_MetadataType]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_MetadataType]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_MetadataType]

	CREATE TABLE [dbo].[RS_MetadataType](
		[TypeId] [int] IDENTITY(1,1) NOT NULL,
		[TypeName] [nchar](32) NOT NULL,
		[Usage] [int] NOT NULL,
	 CONSTRAINT [PK_RS_MetadataType] PRIMARY KEY CLUSTERED 
	(
		[TypeId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

/*
 * [dbo].[RS_Metadata]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Metadata]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Metadata]

	CREATE TABLE [dbo].[RS_Metadata](
		[Id] [int] IDENTITY(1,1) NOT NULL,
		[TypeId] [int] NOT NULL,
		[Name] [nchar](64) NOT NULL,
		[Keyword] [nchar](64) NOT NULL,
		[IsValid] [bit] NOT NULL,
	 CONSTRAINT [PK_RS_Metadata] PRIMARY KEY CLUSTERED 
	(
		[Id] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_Metadata]  WITH CHECK ADD  CONSTRAINT [FK_RS_Metadata_RS_MetadataType] FOREIGN KEY([TypeId])
	REFERENCES [dbo].[RS_MetadataType] ([TypeId])
	ALTER TABLE [dbo].[RS_Metadata] CHECK CONSTRAINT [FK_RS_Metadata_RS_MetadataType]

/*
 * [dbo].[RS_Stock]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Stock]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Stock]

	CREATE TABLE [dbo].[RS_Stock](
		[StockId] [int] NOT NULL,
		[MetadataId] [int] NOT NULL,
		[UnitId] [int] NOT NULL,
		[Number] [decimal](18, 2) NOT NULL,
		[ConvsRate] [decimal](18, 2) NOT NULL,
		[UnitPrice] [decimal](10, 2) NOT NULL,
		[AtomUnitId] [int] NOT NULL,
	 CONSTRAINT [PK_RS_Stock] PRIMARY KEY CLUSTERED 
	(
		[StockId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_Stock]  WITH CHECK ADD  CONSTRAINT [FK_RS_Stock_RS_Metadata] FOREIGN KEY([MetadataId])
	REFERENCES [dbo].[RS_Metadata] ([Id])
	ALTER TABLE [dbo].[RS_Stock] CHECK CONSTRAINT [FK_RS_Stock_RS_Metadata]

	ALTER TABLE [dbo].[RS_Stock]  WITH CHECK ADD  CONSTRAINT [FK_RS_Stock_RS_Unit] FOREIGN KEY([UnitId])
	REFERENCES [dbo].[RS_Unit] ([UnitId])
	ALTER TABLE [dbo].[RS_Stock] CHECK CONSTRAINT [FK_RS_Stock_RS_Unit]

	ALTER TABLE [dbo].[RS_Stock]  WITH CHECK ADD  CONSTRAINT [FK_RS_Stock_RS_Unit1] FOREIGN KEY([AtomUnitId])
	REFERENCES [dbo].[RS_Unit] ([UnitId])
	ALTER TABLE [dbo].[RS_Stock] CHECK CONSTRAINT [FK_RS_Stock_RS_Unit1]

/*
 * [dbo].[RS_MenuItem]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_MenuItem]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_MenuItem]

	CREATE TABLE [dbo].[RS_MenuItem](
		[MenuItemId] [nchar](10) NOT NULL,
		[MetadataId] [int] NOT NULL,
		[UnitId] [int] NOT NULL,
		[UnitPrice] [decimal](18, 2) NOT NULL,
		[Description] [nchar](1024) NULL,
		[LabelIds] [int] NULL,
	 CONSTRAINT [PK_RS_MenuItem] PRIMARY KEY CLUSTERED 
	(
		[MenuItemId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_MenuItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_MenuItem_RS_Metadata] FOREIGN KEY([MetadataId])
	REFERENCES [dbo].[RS_Metadata] ([Id])
	ALTER TABLE [dbo].[RS_MenuItem] CHECK CONSTRAINT [FK_RS_MenuItem_RS_Metadata]

	ALTER TABLE [dbo].[RS_MenuItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_MenuItem_RS_Unit] FOREIGN KEY([UnitId])
	REFERENCES [dbo].[RS_Unit] ([UnitId])
	ALTER TABLE [dbo].[RS_MenuItem] CHECK CONSTRAINT [FK_RS_MenuItem_RS_Unit]

/*
 * [dbo].[RS_MenuItemLabel]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_MenuItemLabel]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_MenuItemLabel]

	CREATE TABLE [dbo].[RS_MenuItemLabel](
	[LabelId] [int] IDENTITY(1,1) NOT NULL,
	[LabelName] [nchar](20) NOT NULL,
	[Image] [image] NOT NULL,
	 CONSTRAINT [PK_RS_MenuItemLabel] PRIMARY KEY CLUSTERED 
	(
		[LabelId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

/*
 * [dbo].[RS_PurchaseBill]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_PurchaseBill]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_PurchaseBill]

	CREATE TABLE [dbo].[RS_PurchaseBill](
		[BillId] [nchar](14) NOT NULL,
		[OperatorId] [bigint] NOT NULL,
		[CheckerId] [bigint] NOT NULL,
		[Amount] [decimal](18, 2) NOT NULL,
		[OccurTime] [datetime] NOT NULL,
		[Status] [int] NOT NULL,
		[Remarks] [nchar](256) NOT NULL,
	 CONSTRAINT [PK_RS_PurchaseBill] PRIMARY KEY CLUSTERED 
	(
		[BillId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_PurchaseBill]  WITH CHECK ADD  CONSTRAINT [FK_RS_PurchaseBill_RS_Employee] FOREIGN KEY([OperatorId])
	REFERENCES [dbo].[RS_Employee] ([UniqueId])
	ALTER TABLE [dbo].[RS_PurchaseBill] CHECK CONSTRAINT [FK_RS_PurchaseBill_RS_Employee]

	ALTER TABLE [dbo].[RS_PurchaseBill]  WITH CHECK ADD  CONSTRAINT [FK_RS_PurchaseBill_RS_Employee1] FOREIGN KEY([CheckerId])
	REFERENCES [dbo].[RS_Employee] ([UniqueId])
	ALTER TABLE [dbo].[RS_PurchaseBill] CHECK CONSTRAINT [FK_RS_PurchaseBill_RS_Employee1]

/*
 * [dbo].[RS_PurchaseBillItem]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_PurchaseBillItem]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_PurchaseBillItem]

	CREATE TABLE [dbo].[RS_PurchaseBillItem](
		[ItemId] [int] IDENTITY(1,1) NOT NULL,
		[UnitId] [int] NOT NULL,
		[UnitPrice] [decimal](10, 2) NOT NULL,
		[Number] [decimal](18, 2) NOT NULL,
		[BillId] [nchar](14) NOT NULL,
		[MetadataId] [int] NOT NULL,
	 CONSTRAINT [PK_RS_PurchaseBillItem] PRIMARY KEY CLUSTERED 
	(
		[ItemId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_PurchaseBillItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_PurchaseBillItem_RS_Metadata] FOREIGN KEY([MetadataId])
	REFERENCES [dbo].[RS_Metadata] ([Id])
	ALTER TABLE [dbo].[RS_PurchaseBillItem] CHECK CONSTRAINT [FK_RS_PurchaseBillItem_RS_Metadata]

	ALTER TABLE [dbo].[RS_PurchaseBillItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_PurchaseBillItem_RS_PurchaseBill] FOREIGN KEY([BillId])
	REFERENCES [dbo].[RS_PurchaseBill] ([BillId])
	ALTER TABLE [dbo].[RS_PurchaseBillItem] CHECK CONSTRAINT [FK_RS_PurchaseBillItem_RS_PurchaseBill]

	ALTER TABLE [dbo].[RS_PurchaseBillItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_PurchaseBillItem_RS_Unit] FOREIGN KEY([UnitId])
	REFERENCES [dbo].[RS_Unit] ([UnitId])
	ALTER TABLE [dbo].[RS_PurchaseBillItem] CHECK CONSTRAINT [FK_RS_PurchaseBillItem_RS_Unit]

/*
 * [dbo].[RS_SalesBill]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_SalesBill]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_SalesBill]

	CREATE TABLE [dbo].[RS_SalesBill](
		[BillId] [nchar](14) NOT NULL,
		[OperatorId] [bigint] NOT NULL,
		[CheckerId] [bigint] NOT NULL,
		[Amount] [decimal](18, 2) NOT NULL,
		[OccurTime] [datetime] NOT NULL,
		[Status] [int] NOT NULL,
		[Remarks] [nchar](256) NOT NULL,
		[TableId] [int] NOT NULL,
	 CONSTRAINT [PK_RS_SalesBill] PRIMARY KEY CLUSTERED 
	(
		[BillId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]	

	ALTER TABLE [dbo].[RS_SalesBill]  WITH CHECK ADD  CONSTRAINT [FK_RS_SalesBill_RS_Employee] FOREIGN KEY([OperatorId])
	REFERENCES [dbo].[RS_Employee] ([UniqueId])
	ALTER TABLE [dbo].[RS_SalesBill] CHECK CONSTRAINT [FK_RS_SalesBill_RS_Employee]

	ALTER TABLE [dbo].[RS_SalesBill]  WITH CHECK ADD  CONSTRAINT [FK_RS_SalesBill_RS_Employee1] FOREIGN KEY([CheckerId])
	REFERENCES [dbo].[RS_Employee] ([UniqueId])
	ALTER TABLE [dbo].[RS_SalesBill] CHECK CONSTRAINT [FK_RS_SalesBill_RS_Employee1]

	ALTER TABLE [dbo].[RS_SalesBill]  WITH CHECK ADD  CONSTRAINT [FK_RS_SalesBill_RS_Table] FOREIGN KEY([TableId])
	REFERENCES [dbo].[RS_Table] ([TableId])
	ALTER TABLE [dbo].[RS_SalesBill] CHECK CONSTRAINT [FK_RS_SalesBill_RS_Table]

/*
 * [dbo].[RS_SalesBillItem]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_SalesBillItem]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_SalesBillItem]

	CREATE TABLE [dbo].[RS_SalesBillItem](
		[ItemId] [int] IDENTITY(1,1) NOT NULL,
		[UnitId] [int] NOT NULL,
		[UnitPrice] [decimal](10, 2) NOT NULL,
		[Number] [decimal](18, 2) NOT NULL,
		[BillId] [nchar](14) NOT NULL,
		[MenuItemId] [nchar](10) NOT NULL,
	 CONSTRAINT [PK_RS_SalesBillItem] PRIMARY KEY CLUSTERED 
	(
		[ItemId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RS_SalesBillItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_SalesBillItem_RS_MenuItem] FOREIGN KEY([MenuItemId])
	REFERENCES [dbo].[RS_MenuItem] ([MenuItemId])
	ALTER TABLE [dbo].[RS_SalesBillItem] CHECK CONSTRAINT [FK_RS_SalesBillItem_RS_MenuItem]

	ALTER TABLE [dbo].[RS_SalesBillItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_SalesBillItem_RS_SalesBill] FOREIGN KEY([BillId])
	REFERENCES [dbo].[RS_SalesBill] ([BillId])
	ALTER TABLE [dbo].[RS_SalesBillItem] CHECK CONSTRAINT [FK_RS_SalesBillItem_RS_SalesBill]

	ALTER TABLE [dbo].[RS_SalesBillItem]  WITH CHECK ADD  CONSTRAINT [FK_RS_SalesBillItem_RS_Unit] FOREIGN KEY([UnitId])
	REFERENCES [dbo].[RS_Unit] ([UnitId])
	ALTER TABLE [dbo].[RS_SalesBillItem] CHECK CONSTRAINT [FK_RS_SalesBillItem_RS_Unit]

/*
 * [dbo].[RS_Charge]
 */
	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RS_Charge]') AND type in (N'U'))
	DROP TABLE [dbo].[RS_Charge]

	CREATE TABLE [dbo].[RS_Charge](
		[ChargeId] [nchar](14) NOT NULL,
		[ChargeItem] [int] NOT NULL,
		[ChargeType] [int] NOT NULL,
		[Amount] [decimal](18, 2) NOT NULL,
		[OccurTime] [datetime] NOT NULL,
		[BillId] [nchar](14) NOT NULL,
		[Remarks] [nchar](1024) NULL,
	 CONSTRAINT [PK_RS_Charge] PRIMARY KEY CLUSTERED 
	(
		[ChargeId] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]